package com.example.springboot01.mapper;


import com.example.springboot01.entity.UserInfo;
import com.example.springboot01.entity.AddressInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AddressInfoMapper {
        @Select("select * from address_info where  address_id =#{address_id}" )
        AddressInfo getAddressById(@Param("address_id") String address_id);
//    @Select("select * from user_info where user_code=#{userCode} " +
//            " and user_pwd=#{userPwd}")
//    UserInfo checkLogin(@Param("userCode") String user_code,
//                        @Param("userPwd") String user_pwd);

//    @Select("SELECT u.*, p.pro_name " +
//            "    FROM user_info u INNER JOIN pro_info p " +
//            "    ON p.pro_id = u.user_pro_id " +
//            "    where ${fieldName} like '%${fieldValue}%'")
//    List<UserInfo> qryUserList(@Param("fieldName") String field_name,
//                               @Param("fieldValue") String field_value);
    //真正工作基本上不允许真的删除这条数据，而应该通过udpate status=0来区分开。
    @Update("UPDATE address_info SET address_status = '0' where address_id=#{address_id}")
    int deleteAddress(@Param("address_id") String address_id);



    //将所有DefultAddress值设置为0
    @Update("UPDATE address_info SET default_address = 0 where user_code=#{userId}")
    int resetAllDefaultAddress(@Param("userId") String userId);

    //寻找最大的address_id并将他设为默认地址
    @Select("SELECT MAX(address_id) FROM address_info where address_status != '0'")
    int getMaxAddressId();
    //将他设为默认地址

    @Update("UPDATE address_info SET default_address = '1' where address_id=#{maxId}")
    int   updateDefaultAddress(@Param("maxId") int maxId);
//    @Update("")
//    int updateUser();

    @Insert("INSERT INTO address_info(receive_man, area, address_detail, postman, telephone, default_address, user_code) " +
            "VALUES (#{addressInfo.receive_man}, #{addressInfo.area}, #{addressInfo.address_detail}, #{addressInfo.postman}, #{addressInfo.telephone}, #{addressInfo.default_address}, #{user_code})")
    int insertAddress(@Param("addressInfo") AddressInfo addressInfo, @Param("user_code") String user_code);
    //#{}里面的名字是来自UserInfo类里面的私有变量
    //user_info()里面的名字是来自数据库里面的列名

//    @Select("select * from user_info where user_code=#{user_code}")
//    UserInfo getAUserInfo(@Param("user_code") String user_code);
//

    @Select("select * from address_info where address_id=#{address_id}")
    AddressInfo getAddressInfo(@Param("address_id") String address_id);
//    @Update("UPDATE address_info SET " +
//            "receive_man = #{addressInfo.receive_man}, " +
//            "area = #{addressInfo.area}, " +
//            "address_detail = #{addressInfo.address_detail}, " +
//            "postman = #{addressInfo.postman}, " +
//            "telephone = #{addressInfo.telephone}, " +
//            "default_address = #{addressInfo.default_address} " +
//            "WHERE address_id = #{addressInfo.address_id}")
//    int editAddress(@Param("addressInfo") AddressInfo addressInfo);

    @Update("update address_info set receive_man=#{receive_man}, area=#{area},address_detail=#{address_detail},postman=#{postman},telephone=#{telephone},default_address=#{default_address}" +
            " where address_id=#{address_id}")
    int editAddress(AddressInfo addressInfo);


//    @Select("SELECT * FROM address_info"+ "limit ${currentPos}, ${pageSize}")
//    List<AddressInfo> qryUserPageList(@Param("currentPos") int current_pos, @Param("pageSize") int page_size);

    @Select("SELECT * FROM address_info WHERE user_code = #{user_code} AND address_status != '0' ORDER BY default_address DESC,address_id DESC LIMIT #{currentPos}, #{pageSize}")
    List<AddressInfo> qryUserPageList(@Param("currentPos") int currentPos, @Param("pageSize") int pageSize, @Param("user_code") String user_code);

}
